package pl.iachoo.ctl.tabory.ui.editors.users;

import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.Section;

import pl.iachoo.ctl.tabory.dao.api.services.IUserService;
import pl.iachoo.ctl.tabory.dao.data.vo.UserVO;
import pl.iachoo.ctl.tabory.dao.hibernate.services.ServiceManager;
import pl.iachoo.ctl.tabory.ui.base.EditorStateSL;
import pl.iachoo.ctl.tabory.ui.base.masterdetail.AbstractMasterDetailSection;
import pl.iachoo.ctl.tabory.ui.properties.Messages;
import pl.iachoo.ctl.tabory.ui.selection.ISelectionListener;

public class UserEditor
		extends
		AbstractMasterDetailSection<UserSelection, UserMasterSectionPart, UserDetailSectionPart> {

	public static final String ID = "pl.iachoo.ctl.tabory.ui.editors.users.UserEditor";

	private static final Logger logger = Logger.getLogger(UserEditor.class);

	public UserEditor() throws Exception {
		super();
	}

	@Override
	protected UserMasterSectionPart getMasterSection(Form parentForm) {
		UserMasterSectionPart sectionPart = new UserMasterSectionPart(
				parentForm.getBody(), formToolkit, Section.TITLE_BAR,
				Messages.getString("users.editor.master.title"), true, true);
		sectionPart.setSelection(selection);
		return sectionPart;
	}

	@Override
	protected UserDetailSectionPart getDetailSection(Form parentForm) {
		UserDetailSectionPart sectionPart = new UserDetailSectionPart(
				parentForm.getBody(), formToolkit, Section.TITLE_BAR,
				Messages.getString("users.editor.detail.title"));
		sectionPart.setSelection(selection);
		return sectionPart;
	}

	@Override
	protected void postInitGui(Composite parent) {
		masterSectionPart
				.addSelectionListener(new ISelectionListener<UserSelection>() {

					@Override
					public void selectionChanged(UserSelection selection) {
						logger.debug("Selection changed");
						EditorStateSL editorStateSL = selection
								.getEditorStateSL();
						if (editorStateSL.equals(EditorStateSL.CLEAN)) {
							setDirty(false);
						} else {
							setDirty(true);
						}
						masterSectionPart.setSelection(selection);
						detailSectionPart.setSelection(selection);
					}
				});

	}

	@Override
	protected void doSave() throws Exception {
		UserSelection saveSelection = detailSectionPart.getSelection();
		UserVO selectedUser = saveSelection.getSelectedUser();
		IUserService userService = ServiceManager.getUserService();
		userService.saveUpdateUser(selectedUser);
		selection = initSelection();
		masterSectionPart.setSelection(selection);
		detailSectionPart.setSelection(selection);
		setDirty(false);
	}

	@Override
	protected void doCancel() throws Exception {
		selection.setSelectedUser(null);
		selection.setEditorStateSL(EditorStateSL.CLEAN);
		masterSectionPart.setSelection(selection);
		detailSectionPart.setSelection(selection);
		setDirty(false);
	}

	@Override
	protected UserSelection initSelection() throws Exception {
		logger.debug("Creating selection");
		UserSelection selection = new UserSelection();
		IUserService userService = ServiceManager.getUserService();
		List<UserVO> listAllUsers = userService.getAllUsers();
		selection.setListAllUsers(listAllUsers);
		selection.setEditorStateSL(EditorStateSL.CLEAN);
		logger.debug("Selection created");
		return selection;
	}

	@Override
	public void setFocus() {
		editorCancelAction.run();
	}
}
